<?php
/**
 * Copyright (c) 2013 Lukas Reschke <lukas@statuscode.ch>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

// Set the content type to Javascript
header("Content-type: text/javascript");

// Disallow caching
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

// Enable l10n support
$l = OC_L10N::get('calendar');

// Get the event sources
$eventSources = array();
$share = JRequest::getString('share','');
$userid = JRequest::getString('userid','');
$calendar_model = new PCalendarModelCalendar();

if ( !empty($userid) ) {
   $calendars = $calendar_model->allCalendars($userid);
} else {
   $calendars = $calendar_model->allCalendars(OC_USER::getUser());
}

foreach($calendars as $calendar) {
   if(!array_key_exists('active', $calendar)){
      $calendar['active'] = 1;
   }
   if($calendar['active'] == 1) {
      $eventSources[] = $calendar_model->getEventSourceInfo($calendar, 0, $share);
   }
}

OCP\Util::emitHook('OC_Calendar', 'getSources', array('sources' => &$eventSources));

$app_model = new PCalendarModelApp();

$array = array(
   "defaultView" => "\"month\"",
   "eventSources" => json_encode($eventSources),
   "categories" => json_encode($app_model->getCategoryOptions()),
   "dayNames" =>  json_encode(array((string)$l->t('Sunday'), (string)$l->t('Monday'), (string)$l->t('Tuesday'), (string)$l->t('Wednesday'), (string)$l->t('Thursday'), (string)$l->t('Friday'), (string)$l->t('Saturday'))),
   "dayNamesShort" =>  json_encode(array((string)$l->t('Sun.'), (string)$l->t('Mon.'), (string)$l->t('Tue.'), (string)$l->t('Wed.'), (string)$l->t('Thu.'), (string)$l->t('Fri.'), (string)$l->t('Sat.'))),
   "monthNames" =>  json_encode(array((string)$l->t('January'), (string)$l->t('February'), (string)$l->t('March'), (string)$l->t('April'), (string)$l->t('May'), (string)$l->t('June'), (string)$l->t('July'), (string)$l->t('August'), (string)$l->t('September'), (string)$l->t('October'), (string)$l->t('November'), (string)$l->t('December'))),
   "monthNamesShort" =>  json_encode(array((string)$l->t('Jan.'), (string)$l->t('Feb.'), (string)$l->t('Mar.'), (string)$l->t('Apr.'), (string)$l->t('May.'), (string)$l->t('Jun.'), (string)$l->t('Jul.'), (string)$l->t('Aug.'), (string)$l->t('Sep.'), (string)$l->t('Oct.'), (string)$l->t('Nov.'), (string)$l->t('Dec.'))),
   "agendatime" =>  "\"".((int) OCP\Config::getUserValue(OC_USER::getUser(), 'calendar', 'timeformat', '24') == 24 ? 'HH:mm' : 'hh:mm tt')."{ -".((int) OCP\Config::getUserValue(OC_USER::getUser(), 'calendar', 'timeformat', '24') == 24 ? 'HH:mm' : 'hh:mm tt')."}"."\"",
   "defaulttime" => "\"".((int) OCP\Config::getUserValue(OC_USER::getUser(), 'calendar', 'timeformat', '24') == 24 ? 'HH:mm' : 'hh:mm tt')."\"",
   "totalurl" => "\"".OCP\Util::linkToRemote('caldav')."calendars"."\"",
   "firstDay" => 1,
   "login_url" => "\"".OC_Helper::linkToRoute('company_login')."\"",
   "joblist_url" => "\"".OC_Helper::linkToRoute('calendar_joblist')."\"",
   "remindfeedback_url" => "\"".OC_Helper::linkToRoute('calendar_interviewfeedbackremind')."\"",
);

// Echo it
foreach ($array as  $setting => $value) {
   echo("var ". $setting ."=".$value.";\n");
}
